package com.ymatou.query.seller.trading.api.parameter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.database.sqlwapper.YmtUsersWapper;
import com.ymttest.utils.DataUtil;

/**
 * 存储过程: sp_trd_GetSellerOrderLeaveWordByProduct
 * 
 * @author songyefeng
 *
 */
public class Sp_GetSellerOrderLeaveWordByProduct {
	private Integer sellerId;
	private Integer lastOrderId;
	private String productId;
	private List<Integer> orderStatus;
	private Integer top;
	private Boolean considerOrderStatus;
	private Boolean considerRCOrderEstablish;
	private Boolean considerRCAccountPaid;
	private Boolean considerRestOrderStatus;

	public Integer getSellerId() {
		return sellerId;
	}

	public void setSellerId(Integer sellerId) {
		this.sellerId = sellerId;
	}

	public Integer getLastOrderId() {
		return lastOrderId;
	}

	public void setLastOrderId(Integer lastOrderId) {
		this.lastOrderId = lastOrderId;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public List<Integer> getOrderStatus() {
		return orderStatus;
	}

	public void setOrderStatus(List<Integer> orderStatus) {
		this.orderStatus = orderStatus;
	}

	public Integer getTop() {
		return top;
	}

	public void setTop(Integer top) {
		this.top = top;
	}

	public Boolean getConsiderOrderStatus() {
		return considerOrderStatus;
	}

	public void setConsiderOrderStatus(Boolean considerOrderStatus) {
		this.considerOrderStatus = considerOrderStatus;
	}

	public Boolean getConsiderRCOrderEstablish() {
		return considerRCOrderEstablish;
	}

	public void setConsiderRCOrderEstablish(Boolean considerRCOrderEstablish) {
		this.considerRCOrderEstablish = considerRCOrderEstablish;
	}

	public Boolean getConsiderRCAccountPaid() {
		return considerRCAccountPaid;
	}

	public void setConsiderRCAccountPaid(Boolean considerRCAccountPaid) {
		this.considerRCAccountPaid = considerRCAccountPaid;
	}

	public Boolean getConsiderRestOrderStatus() {
		return considerRestOrderStatus;
	}

	public void setConsiderRestOrderStatus(Boolean considerRestOrderStatus) {
		this.considerRestOrderStatus = considerRestOrderStatus;
	}

	private List<Ymt_Order> orderList = new ArrayList<Ymt_Order>();
	private List<Ymt_OrderInfo> orderInfoList = new ArrayList<Ymt_OrderInfo>();
	private List<Ymt_Users> userList = new ArrayList<Ymt_Users>();

	public List<Ymt_Order> getOrderList() {
		return orderList;
	}

	public void setOrderList(List<Ymt_Order> orderList) {
		this.orderList = orderList;
	}

	public List<Ymt_OrderInfo> getOrderInfoList() {
		return orderInfoList;
	}

	public void setOrderInfoList(List<Ymt_OrderInfo> orderInfoList) {
		this.orderInfoList = orderInfoList;
	}

	public List<Ymt_Users> getUserList() {
		return userList;
	}

	public void setUserList(List<Ymt_Users> userList) {
		this.userList = userList;
	}

	private YmtOrdersWapper ymtOrdersWapper = new YmtOrdersWapper();
	private DefaultWapper defaultWapper = new DefaultWapper();
	private YmtUsersWapper ymtUsersWapper = new YmtUsersWapper();

	@SuppressWarnings("rawtypes")
	public void Execute() {
		List<Map> orderIdsAndUserIds;
		String queryField = " top " + top + " o.iOrderId, o.iUserId";
		StringBuffer queryCondition = new StringBuffer();
		queryCondition.append(" o.iBuyerId =" + sellerId);
		if (lastOrderId != null) {
			queryCondition.append(" and o.iOrderId < " + lastOrderId);
		}
		if (DataUtil.isNullOrEmpty(orderStatus) == false) {
			queryCondition.append(" and o.iTradingStatus in "
					+ DataUtil.toInOfSql(orderStatus));
		}
		queryCondition.append(" order by o.dAddTime desc");
		orderIdsAndUserIds = ymtOrdersWapper.selectOrders2(queryField, null,
				queryCondition.toString());
		String joinCondition = null;
		if (productId != null) {
			joinCondition = " join Ymt_OrderInfo(nolock) i on o.iOrderId = i.iOrderId and i.sProductId = '"
					+ productId + "'";
		}
		orderIdsAndUserIds = ymtOrdersWapper.selectOrders2(queryField,
				joinCondition, queryCondition.toString());

		List<Map> orders = null;
		List<Map> orderinfos = null;
		List<Map> users = null;
		if (orderIdsAndUserIds.size() > 0) {
			List orderIds = new ArrayList();
			List userIds = new ArrayList();
			for (Map m : orderIdsAndUserIds) {
				orderIds.add(m.get("iOrderId"));
				userIds.add(m.get("iUserId"));
			}
			String queryField1 = " iOrderId,iUserId,sLeaveWord,dAddTime";
			String queryCondition1 = " iOrderId in "
					+ DataUtil.toInOfSql(orderIds)+" order by dAddTime desc";
			orders = ymtOrdersWapper.selectOrders2(queryField1, null,
					queryCondition1);

			String queryField2 = " iOrderId, sPropertyInfo, iPriceType";
			String queryCondition2 = queryCondition1;
			orderinfos = defaultWapper.selectOrderInfo(queryField2, null,
					queryCondition2);

			String queryField3 = " iUserId, sLoginId";
			String queryCondition3 = " iUserId in "
					+ DataUtil.toInOfSql(userIds);
			users = ymtUsersWapper.selectUsers(queryField3, null,
					queryCondition3);
		}
		if (orders != null) {
			for (Map m : orders) {
				Ymt_Order o = new Ymt_Order();
				o.setiOrderId((int) m.get("iOrderId"));
				o.setiUserId((int) m.get("iUserId"));
				o.setsLeaveWord((String) m.get("sLeaveWord"));
				o.setdAddTime(String.valueOf(m.get("dAddTime")));
				orderList.add(o);
			}
		}
		if (orderinfos != null) {
			for (Map m : orderinfos) {
				Ymt_OrderInfo o = new Ymt_OrderInfo();
				o.setiOrderId((int) m.get("iOrderId"));
				o.setsPropertyInfo(String.valueOf(m.get("sPropertyInfo")));
				o.setiPriceType(DataUtil.Integerr.GetValueOrDefault(m
						.get("iPriceType")));
				orderInfoList.add(o);
			}
		}
		if (users != null) {
			for (Map m : users) {
				Ymt_Users o = new Ymt_Users();
				o.setiUserId((int) m.get("iUserId"));
				o.setsLoginId(String.valueOf(m.get("sLoginId")));
				userList.add(o);
			}
		}
	}
}
